package com.bicon.botu.finance.payment.weixin.api;

import com.bicon.botu.finance.payment.weixin.utils.CodeUtil;
import com.bicon.botu.finance.payment.weixin.utils.PayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * @Email javen205@126.com
 * @author Javen
 * 2017年5月22日
 */
public class WxPayApiConfig {



	/**
	 * 生成请求微信支付的相关参数和签名
	 **/
	public static String createBookingParams(String code, String appid, String partner, String productTitle, String orderNumber,
											 float total, String notifyUri, String nonceStr, String key) {
		if (productTitle.length() > 16) {
			productTitle = productTitle.substring(0, 15);
		}
		List<NameValuePair> packageParams = new ArrayList<NameValuePair>();
		packageParams.add(new BasicNameValuePair("appid", appid));
		packageParams.add(new BasicNameValuePair("attach", code));
		packageParams.add(new BasicNameValuePair("body", productTitle));
		packageParams.add(new BasicNameValuePair("mch_id", partner));
		packageParams.add(new BasicNameValuePair("nonce_str", nonceStr));
		packageParams.add(new BasicNameValuePair("notify_url", notifyUri));

		packageParams.add(new BasicNameValuePair("out_trade_no", orderNumber));
		packageParams.add(new BasicNameValuePair("spbill_create_ip", "116.62.180.74"));
		packageParams.add(new BasicNameValuePair("total_fee", (int) (total * 100) + ""));
		packageParams.add(new BasicNameValuePair("trade_type", "APP"));

		String sign = PayUtils.genPackageSign(packageParams, key);
		packageParams.add(new BasicNameValuePair("sign", sign));
		String xmlstring = PayUtils.toXml(packageParams);

		return xmlstring;
	}


	/**
	 * 生成请求微信支付订单查询的相关参数和签名
	 **/
	public static String createQueryOrderParams(String appid, String partner, String orderNo, String nonceStr, String key) {
		List<NameValuePair> packageParams = new ArrayList<NameValuePair>();
		packageParams.add(new BasicNameValuePair("appid", appid));
		packageParams.add(new BasicNameValuePair("mch_id", partner));
		packageParams.add(new BasicNameValuePair("nonce_str", nonceStr));
		packageParams.add(new BasicNameValuePair("out_trade_no", orderNo));
		String sign = PayUtils.genPackageSign(packageParams, key);
		packageParams.add(new BasicNameValuePair("sign", sign));
		String xmlstring = PayUtils.toXml(packageParams);
		return xmlstring;
	}

	/**
	 * 生成请求微信提现的相关参数和签名
	 *
	 * @param appid      公众账号appid
	 * @param mchid      mchid
	 * @param deviceInfo 设备号
	 * @param nonceStr   随机字符串
	 * @param transferId 商户提现订单号
	 * @param openId     用户openid
	 * @param payeeName  收款用户姓名
	 * @param amount     金额
	 * @param remark     企业付款描述信息
	 * @param key        微信APP 加密KEY
	 * @return
	 */
	public static String createWeChatTransferParams(String appid, String mchid, String deviceInfo, String nonceStr, String transferId,
													String openId, String payeeName, String amount, String remark, String key) {
		List<NameValuePair> packageParams = new ArrayList<NameValuePair>();
		packageParams.add(new BasicNameValuePair("mch_appid", appid));                                   //微信分配的公众账号ID（企业号corpid即为此appId）
		packageParams.add(new BasicNameValuePair("mchid", mchid));                                       //微信支付分配的商户号
		packageParams.add(new BasicNameValuePair("device_info", deviceInfo));                            //微信支付分配的终端设备号
		packageParams.add(new BasicNameValuePair("nonce_str", nonceStr));                                //随机字符串，不长于32位
		packageParams.add(new BasicNameValuePair("partner_trade_no", transferId));                       //商户订单号，需保持唯一性(只能是字母或者数字，不能包含有符号)
		packageParams.add(new BasicNameValuePair("openid", openId));                                     //商户appid下，某用户的openid
		packageParams.add(new BasicNameValuePair("check_name", "FORCE_CHECK"));                   //校验用户姓名选项
		packageParams.add(new BasicNameValuePair("re_user_name", payeeName));                            //收款用户姓名

		amount = (int) (Double.valueOf(amount) * 100) + "";
		packageParams.add(new BasicNameValuePair("amount", amount));                                     //金额:企业付款金额，单位为分
		packageParams.add(new BasicNameValuePair("desc", remark));                                       //企业付款描述信息:企业付款金额，必填。
		packageParams.add(new BasicNameValuePair("spbill_create_ip", "116.62.180.74"));           //Ip地址:调用接口的机器Ip地址

		String sign = PayUtils.genPackageSign(packageParams, key);
		packageParams.add(new BasicNameValuePair("sign", sign));                                         //签名
		String xmlstring = PayUtils.toXml(packageParams);
		return xmlstring;
	}

	/**
	 * 生成微信提现查询的相关参数和签名
	 * @param nonceStr
	 * @param appid
	 * @param mchid
	 * @param transferId
	 * @param key
	 * @return
	 */
	public static String createWeChatTransferinfoParams(String appid, String mchid, String nonceStr, String transferId, String key) {
		List<NameValuePair> packageParams = new ArrayList<NameValuePair>();
		packageParams.add(new BasicNameValuePair("mch_appid", appid));                                   //微信分配的公众账号ID（企业号corpid即为此appId）
		packageParams.add(new BasicNameValuePair("mchid", mchid));                                       //微信支付分配的商户号
		packageParams.add(new BasicNameValuePair("nonce_str", nonceStr));                                //随机字符串，不长于32位
		packageParams.add(new BasicNameValuePair("partner_trade_no", transferId));                       //商户订单号，需保持唯一性(只能是字母或者数字，不能包含有符号)
		String sign = PayUtils.genPackageSign(packageParams, key);
		packageParams.add(new BasicNameValuePair("sign", sign));                                         //签名
		String xmlstring = PayUtils.toXml(packageParams);
		return xmlstring;
	}

	/**
	 * @param appid						appID
	 * @param mch_id					商户号
	 * @param transaction_id			微信订单号
	 * @param out_trade_no				商户订单号
	 * @param out_refund_no				商户退款单号
	 * @param total_fee					订单金额  单位为分
	 * @param refund_fee				退款金额  单位为分
	 * @param refund_desc				若商户传入
	 * @param notify_url				退款结果通知url
	 * @return
	 */
	public static String createRefundParam(String appid, String mch_id, String transaction_id, String out_trade_no, String out_refund_no,
										   long total_fee, long refund_fee,String refund_desc, String notify_url,String appkey) {
		String nonce_str = CodeUtil.Code(25);
		List<NameValuePair> packageParams = new ArrayList<NameValuePair>();
		packageParams.add(new BasicNameValuePair("appid",appid));
		packageParams.add(new BasicNameValuePair("mch_id",mch_id));
		packageParams.add(new BasicNameValuePair("nonce_str",nonce_str));
		if(StringUtils.isNotBlank(transaction_id)){
			packageParams.add(new BasicNameValuePair("transaction_id",transaction_id));
		}
		if(StringUtils.isNotBlank(out_trade_no)) {
			packageParams.add(new BasicNameValuePair("out_trade_no", out_trade_no));
		}
		packageParams.add(new BasicNameValuePair("out_refund_no",out_refund_no));
		packageParams.add(new BasicNameValuePair("total_fee",total_fee+""));
		packageParams.add(new BasicNameValuePair("refund_fee",refund_fee+""));
		packageParams.add(new BasicNameValuePair("refund_desc",refund_desc));
		if(StringUtils.isNotBlank(notify_url)) {
			packageParams.add(new BasicNameValuePair("notify_url", notify_url));
		}
		String sign = PayUtils.genPackageSign(packageParams, appkey);
		packageParams.add(new BasicNameValuePair("sign",sign));
		String xmlstring = PayUtils.toXml(packageParams);
		return xmlstring;
	}
}
